package com.food.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.food.admin.dto.FoodInfoDto;
import com.food.admin.dto.StoreInfoDetailDto;
import com.food.admin.dto.StoreInfoListDto;
import com.food.admin.entity.*;
import com.food.admin.exception.storeInfoException.StoreInfoException;
import com.food.admin.exception.storeInfoException.StoreInfoExceptionCode;
import com.food.admin.form.StoreInfoIdForm;
import com.food.admin.form.StoreInfoListForm;
import com.food.admin.form.StoreInfoStateUpdateForm;
import com.food.admin.mapper.StoreInfoMapper;
import com.food.admin.model.ConstantData;
import com.food.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.admin.util.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家信息 服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {
    @Autowired
    private StoreInfoMapper storeInfoMapper;
    @Autowired
    private DietTimeConfigService dietTimeConfigService;
    @Autowired
    private ReceiveTimeConfigService receiveTimeConfigService;
    @Autowired
    private FoodInfoService foodInfoService;
    @Autowired
    private FoodMonthSaleService foodMonthSaleService;
    @Autowired
    private StoreAttachmentService storeAttachmentService;
    @Override
    public MyPage<StoreInfoListDto> listByPageAndCondition(StoreInfoListForm form) {
        QueryWrapper<StoreInfo> storeInfoQueryWrapper = new QueryWrapper<>();

        //商铺名或联系人模糊查询
        if (form.getCondition()!=null&&form.getCondition()!=""){
            storeInfoQueryWrapper.like("store_info_name",form.getCondition()).or()
                    .like("store_info_link_man",form.getCondition());
        }
        Page<StoreInfo> storeInfoPage = new Page<>(form.getPageIndex(),form.getPageSize());
        storeInfoMapper.selectPage(storeInfoPage,storeInfoQueryWrapper);
        MyPage<StoreInfoListDto> listDtoMyPage = new MyPage<>();
        //装配
        listDtoMyPage.setPages(storeInfoPage.getPages());
        listDtoMyPage.setPageIndex(storeInfoPage.getCurrent());
        listDtoMyPage.setPageSize(storeInfoPage.getSize());
        listDtoMyPage.setTotal(storeInfoPage.getTotal());
        ArrayList<StoreInfoListDto> storeRegisterListDtos = new ArrayList<>();

        for (StoreInfo record : storeInfoPage.getRecords()) {
            StoreInfoListDto storeInfoListDto = BeanUtil.toBean(record, StoreInfoListDto.class);
            if (storeInfoListDto.getStoreInfoStatus()== ConstantData.STORE_STATE_NORMAL){
                storeInfoListDto.setStoreInfoStatusName("激活");
            }else {
                storeInfoListDto.setStoreInfoStatusName("冻结");
            }

            if (storeInfoListDto.getStoreInfoStoreStatus()==ConstantData.STORE_BUSY_STATE_NORMAL){
                storeInfoListDto.setStoreInfoStoreStatusName("开业");
            }else {
                storeInfoListDto.setStoreInfoStoreStatusName("歇业");
            }
            storeRegisterListDtos.add(storeInfoListDto);
        }
        listDtoMyPage.setList(storeRegisterListDtos);
        return listDtoMyPage;
    }

    @Override
    public StoreInfoDetailDto getDetail(StoreInfoIdForm form) {
        StoreInfo storeInfo = getById(form.getStoreInfoId());
        if (storeInfo==null){
            throw new StoreInfoException(StoreInfoExceptionCode.STORE_INFO_IS_NOT_EXIST);
        }
        //装配商家基础信息详情
        StoreInfoDetailDto detailDto = new StoreInfoDetailDto();
        detailDto.setStoreInfo(storeInfo);
        if (storeInfo.getStoreInfoStatus()== ConstantData.STORE_STATE_NORMAL){
            detailDto.setStoreInfoStatusName("激活");
        }else {
            detailDto.setStoreInfoStatusName("冻结");
        }

        if (storeInfo.getStoreInfoStoreStatus()==ConstantData.STORE_BUSY_STATE_NORMAL){
            detailDto.setStoreInfoStoreStatusName("开业");
        }else {
            detailDto.setStoreInfoStoreStatusName("歇业");
        }
        //装配用餐表
        List<DietTimeConfig> dietTimeConfigs = dietTimeConfigService.list(new QueryWrapper<DietTimeConfig>().eq("diet_time_config_store_id", storeInfo.getStoreInfoId()));
        detailDto.setDietTimeConfigs(dietTimeConfigs);
        //装配送餐配置
        List<ReceiveTimeConfig> receiveTimeConfigs = receiveTimeConfigService.list(new QueryWrapper<ReceiveTimeConfig>().eq("receive_time_config_store_id", storeInfo.getStoreInfoId()));
        detailDto.setReceiveTimeConfigs(receiveTimeConfigs);
        //装配该商家菜品
        List<FoodInfoDto> foodInfoDtos=new ArrayList<>();
        List<FoodInfo> foods = foodInfoService.list(new QueryWrapper<FoodInfo>().eq("food_info_store_id", storeInfo.getStoreInfoId()));
        for (FoodInfo food : foods) {
            FoodInfoDto foodInfoDto = BeanUtil.toBean(food, FoodInfoDto.class);
            //装配月销量
            //获取当前年月
            YearMonth yearMonth=YearMonth.from(LocalDate.now());
            System.out.println(yearMonth.toString());
            FoodMonthSale one = foodMonthSaleService
                    .getOne(new QueryWrapper<FoodMonthSale>()
                            .eq("food_month_sale_store_id", storeInfo.getStoreInfoId())
                            .eq("food_month_sale_food_id", food.getFoodInfoId())
                            .eq("food_month_sale_date_str", yearMonth));
            if (one==null){
                foodInfoDto.setFoodMonthSaleCount(0);
            }else {
                foodInfoDto.setFoodMonthSaleCount(one.getFoodMonthSaleCount());
            }
            foodInfoDtos.add(foodInfoDto);
        }
        detailDto.setFoodInfoDtos(foodInfoDtos);
        //装配附件表
        List<StoreAttachment> attachments = storeAttachmentService.list(new QueryWrapper<StoreAttachment>().eq("store_attachment_store_id", storeInfo.getStoreInfoId()));
        detailDto.setAttachments(attachments);
        return detailDto;
    }

    @Override
    public void updateState(StoreInfoStateUpdateForm form) {
        StoreInfo storeInfo = getById(form.getStoreInfoId());
        if (storeInfo==null){
            throw new StoreInfoException(StoreInfoExceptionCode.STORE_INFO_IS_NOT_EXIST);
        }
        if (form.getStateId()==ConstantData.STORE_STATE_NORMAL){
            //要激活
            if (storeInfo.getStoreInfoStatus()==ConstantData.STORE_STATE_NORMAL){
                throw new StoreInfoException(StoreInfoExceptionCode.STORE_STATE_ALREADY_NORMAL);
            }
            storeInfo.setStoreInfoStatus(ConstantData.STORE_STATE_NORMAL);
            updateById(storeInfo);
        }else if (form.getStateId()==ConstantData.STORE_STATE_DISABLE){
            //要冻结
            if (storeInfo.getStoreInfoStatus()==ConstantData.STORE_STATE_DISABLE){
                throw new StoreInfoException(StoreInfoExceptionCode.STORE_STATE_ALREADY_DISABLE);
            }
            storeInfo.setStoreInfoStatus(ConstantData.STORE_STATE_DISABLE);
            //强制商铺歇业
            storeInfo.setStoreInfoStoreStatus(ConstantData.STORE_BUSY_STATE_DISABLE);
            updateById(storeInfo);
        }
    }
}
